﻿/*
 * Created by SharpDevelop.
 * User: IEscarro
 * Date: 8/25/2009
 * Time: 9:58 AM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using NUnit.Framework;
using Miming.Model;

namespace Miming.Tests
{
	[TestFixture]
	public class Test1
	{
		[SetUpAttribute]
		public void TestFixture()
		{
			User u = SessionFacade.ReadUserByNameAndPassword("admin", "root");
			if (u == null) {
				u = new User();
				u.Name = "admin";
				u.Password = "root";
				SessionFacade.SaveOrUpdateUser(u);
			}
			
			Company c = SessionFacade.ReadCompanyByPk(1);
			if (c == null) {
				c = new Company();
				c.Name = "Company 1";
				c.Address = "";
				c.Country = 1;
				c.Phone = "";
				c.Fax = "";
				c.Email = "";
				c.Website = "";
				c.LegalName = "";
				c.LegalAddress = new Address("", "", "", "", "", "");
				c.State = 1;
				c.LegalCountry = 1;
				c.FiscalYear = 1;
				c.TaxYear = 1;
				c.IncomeTaxForm = 1;
				c.EIN = "";
				c.SSN = "";
				c.PayrollInfo = new PayrollInfo("", "", "");
				c.ShipToName = "";
				c.ShipToAddress = "";
				SessionFacade.SaveOrUpdateCompany(c);
			}
		}
		
		[Test]
		public void TestPurchaseModule()
		{
			User u = SessionFacade.ReadUserByNameAndPassword("admin", "root");
			if (u == null) {
				Console.WriteLine("User not found!");
				return;
			}
			
			UnitOfMeasure un = new UnitOfMeasure();
			un.SetName = "Count in each";
			un.Name = "Each";
			un.Abbreviation = "ea";
			// TODO: Related units
			// TODO: Default units
			un.Inactive = false;
			SessionFacade.SaveOrUpdateUnitOfMeasure(un);
			
			Account a = new Account();
			a.Name = "Account 1";
			a.Parent = null;
			a.Description = "";
			a.Note = "";
			a.CreditCardNo = "";
			a.AccountNo = "";
			a.RoutingNumber = "";
			a.TaxLineMapping = 1;
			a.Inactive = false;
			SessionFacade.SaveOrUpdateAccount(a);
			
			VendorType vt = new VendorType();
			vt.Name = "";
			vt.Parent = null;
			vt.Inactive = false;
			SessionFacade.SaveOrUpdateVendorType(vt);
			
			Terms terms = new Terms();
			terms.Name = "";
			// TODO: Type
			terms.Inactive = false;
			SessionFacade.SaveOrUpdateTerms(terms);
			
			BillingRateLevel b = new BillingRateLevel();
			b.Name = "";
			// TODO:
			SessionFacade.SaveOrUpdateBillingRateLevel(b);
			
			Vendor v = new Vendor();
			v.Name = "Vendor 1";
			v.OpeningBalance = 0;
			v.BalanceAsOf = DateTime.Now;
			v.CompanyName = "";
			v.LegalName = new Name("", "", "", "");
			v.Address = "";
			v.Contact = "";
			v.Phone = "";
			v.Fax = "";
			v.AltPhone = "";
			v.AltContact = "";
			v.Email = "";
			v.CC = "";
			v.PrintOnCheckAs = "";
			v.AccountNo = "";
			v.VendorType = vt;
			v.Terms = terms;
			v.CreditLimit = 0;
			v.TaxId = "";
			v.EligibleFor1099 = false;;
			v.BillingRateLevel = b;
			v.AccountPrefill1 = a;
			v.AccountPrefill2 = a;
			v.AccountPrefill3 = a;
			v.Inactive = false;
			SessionFacade.SaveOrUpdateVendor(v);
			
			SalesTaxCode t = new SalesTaxCode();
			t.Code = "CC";
			t.Description = "";
			t.Taxable = true;
			SessionFacade.SaveOrUpdateSalesTaxCode(t);
			
			Item i = new Item();
			i.Name = "Item 1";
			i.PartNumber = "";
			i.UnitOfMeasure = un;
			i.PurchaseDescription = "";
			i.Cost = 0;
			i.COGSAccount = a;
			i.PreferredVendor = v;
			i.SalesDescription = "";
			i.SalesPrice = 0;
			i.TaxCode = t;
			i.IncomeAccount = a;
			i.AssetAccount = a;
			i.ReorderPoint = 0;
			i.OnHand = 0;
			i.TotalValue = 0;
			i.ValueAsOf = DateTime.Now;
			i.Notes = "";
			
			Item i2 = new Item();
			i2.Name = "Item 2";
			Item i3 = new Item();
			i3.Name = "Item 3";
			
			SessionFacade.SaveOrUpdateItem(i);
			SessionFacade.SaveOrUpdateItem(i2);
			SessionFacade.SaveOrUpdateItem(i3);
			
			Class cl = new Class();
			cl.Name = "Class 1";
			cl.Parent = null;
			cl.Inactive = false;
			SessionFacade.SaveOrUpdateClass(cl);
			
			PurchaseOrder o = new PurchaseOrder();
			o.Vendor = v;
			o.Class = cl;
			o.VendorDetails = v.ToString();
			o.ShipTo = "";
			o.AddItem(new PurchaseOrderItem(i));
			o.AddItem(new PurchaseOrderItem(i2));
			o.AddItem(new PurchaseOrderItem(i3));
			o.Message = "";
			o.Memo = "";
			SessionFacade.SaveOrUpdatePurchaseOrder(o);
			
			ItemReceipt r = new ItemReceipt();
			r.Vendor = v;
			r.Date = DateTime.Now;
			r.RefNo = "";
			r.Memo = "";
			r.AddItem(new ItemReceiptItem(i));
			r.AddItem(new ItemReceiptItem(i2));
			r.AddItem(new ItemReceiptItem(i3));
			SessionFacade.SaveOrUpdateItemReceipt(r);
			
			Bill bl = new Bill();
			bl.BillReceived = true;
			bl.Vendor = v;
			bl.VendorDetails = v.ToString();
			bl.Terms = terms;
			bl.Memo = "";
			bl.Date = DateTime.Now;
			bl.RefNo = "";
			bl.BillDue = DateTime.Now.AddDays(5);
			bl.AddItem(new BillItem(i));
			bl.AddItem(new BillItem(i2));
			bl.AddItem(new BillItem(i3));
			SessionFacade.SaveOrUpdateBill(bl);
		}
		
		[Test]
		public void TestSalesModule()
		{
			User u = SessionFacade.ReadUserByNameAndPassword("admin", "root");
			if (u == null) {
				Console.WriteLine("User not found!");
				return;
			}
			
			PaymentMethod p = new PaymentMethod();
			p.Name = "";
			p.Type = 1;
			p.Inactive = false;
			SessionFacade.SaveOrUpdatePaymentMethod(p);
			
			PriceLevel pl = new PriceLevel();
			pl.Name = "";
			pl.Inactive = false;
			SessionFacade.SaveOrUpdatePriceLevel(pl);
			
			CustomerType ct = new CustomerType();
			ct.Name = "";
			ct.Parent = null;
			ct.Inactive = false;
			SessionFacade.SaveOrUpdateCustomerType(ct);
			
			JobType j = new JobType();
			j.Name = "";
			j.Parent = null;
			j.Inactive = false;
			SessionFacade.SaveOrUpdateJobType(j);
			
			Customer c = new Customer();
			c.Name = "";
			c.OpeningBalance = 0;
			c.BalanceAsOf = DateTime.Now;
			c.CompanyName = "";
			c.LegalName = new Name("", "", "", "");
			c.BillToAddress = "";
			// TODO: Ship To
			c.Contact = "";
			c.Phone = "";
			c.Fax = "";
			c.AltPhone = "";
			c.AltContact = "";
			c.Email = "";
			c.CC = "";
			c.CustomerType = ct;
			c.Terms = SessionFacade.ReadTermsByPk(1);
			// TODO: Sales rep
			c.SendMethod = 1;
			c.TaxCode = SessionFacade.ReadSalesTaxCodeByPk(1);
			c.TaxItem = SessionFacade.ReadItemByPk(1);
			c.ResaleNumber = "";
			c.PriceLevel = pl;
			c.AccountNo = "";
			c.CreditLimit = 0;
			c.PreferredPaymentMethod = p;
			c.CreditCardInfo = new CreditCardInfo("", "", "", "", "", "");
			c.JobInfo = new JobInfo(1, DateTime.Now, DateTime.Now, DateTime.Now, "");
			c.JobType = j;
			c.Inactive = false;
			SessionFacade.SaveOrUpdateCustomer(c);
			
			CustomerMessage cm = new CustomerMessage();
			cm.Message = "";
			cm.Inactive = false;
			SessionFacade.SaveOrUpdateCustomerMessage(cm);
			
			SalesOrder o = new SalesOrder();
			o.Customer = c;
			o.Date = DateTime.Now;
			o.Class = SessionFacade.ReadClassByPk(1);
			o.SONo = "";
			o.NameAddress = "";
			o.ShipTo = "";
			o.PONo = "";
			o.AddItem(new SalesOrderItem(SessionFacade.ReadItemByPk(1)));
			o.AddItem(new SalesOrderItem(SessionFacade.ReadItemByPk(2)));
			o.AddItem(new SalesOrderItem(SessionFacade.ReadItemByPk(3)));
			o.CustomerMessage = cm;
			o.TaxItem = SessionFacade.ReadItemByPk(1);
			o.CustomerTaxCode = SessionFacade.ReadSalesTaxCodeByPk(1);
			o.Memo = "";
			SessionFacade.SaveOrUpdateSalesOrder(o);
			
			SalesReceipt s = new SalesReceipt();
			s.Customer = c;
			s.Class = SessionFacade.ReadClassByPk(1);
			s.Date = DateTime.Now;
			s.SaleNo = "";
			s.SoldTo = "";
			s.CheckNo = "";
			s.PaymentMethod = p;
			s.AddItem(new SalesReceiptItem(SessionFacade.ReadItemByPk(1)));
			s.AddItem(new SalesReceiptItem(SessionFacade.ReadItemByPk(2)));
			s.AddItem(new SalesReceiptItem(SessionFacade.ReadItemByPk(3)));
			s.CustomerMessage = cm;
			s.TaxItem = SessionFacade.ReadItemByPk(1);
			s.CustomerTaxCode = SessionFacade.ReadSalesTaxCodeByPk(1);
			s.Memo = "";
			
			Invoice i = new Invoice();
			i.Customer = c;
			i.Date = DateTime.Now;
			i.InvoiceNo = "";
			i.BillTo = "";
			i.ShipTo = "";
			i.Terms = SessionFacade.ReadTermsByPk(1);
			i.DueDate = DateTime.Now.AddDays(5);
			i.CustomerMessage = cm;
			i.Tax = SessionFacade.ReadItemByPk(1);
			i.CustomerTaxCode = SessionFacade.ReadSalesTaxCodeByPk(1);
			i.Memo = "";
		}
		
		[Test]
		public void TestSystemModule()
		{
			if (SessionFacade.ReadModuleByCode("purchase") == null) {
				Module m = new Module();
				m.Code = "purchase";
				m.Name = "Purchase Module";
				m.AddPrivilege(new Privilege("Create New Item", "Miming.Commands.CreateNewItem"));
				m.AddPrivilege(new Privilege("Create New Vendor", "Miming.Commands.CreateNewVendor"));
				m.AddPrivilege(new Privilege("Create Purchase Orders", "Miming.Commands.CreatePurchaseOrders"));
				SessionFacade.SaveOrUpdateModule(m);
			}
			if (SessionFacade.ReadModuleByCode("sales") == null) {
				Module m = new Module();
				m.Code = "sales";
				m.Name = "Sales Module";
				m.AddPrivilege(new Privilege("Create New Customer", "Miming.Commands.CreateNewCustomer"));
				m.AddPrivilege(new Privilege("Create Sales Orders", "Miming.Commands.CreateSalesOrders"));
				SessionFacade.SaveOrUpdateModule(m);
			}
		}
		
		[Test]
		public void TestHumanResourceModule()
		{
			User u = SessionFacade.ReadUserByNameAndPassword("admin", "root");
			if (u == null) {
				Console.WriteLine("User not found!");
				return;
			}
			
			WorkersCompensation w = new WorkersCompensation();
			w.Code = "";
			w.Description = "";
			w.Rate = 0;
			w.StartUsingOn = DateTime.Now.AddMonths(3);
			w.Inactive = false;
			SessionFacade.SaveOrUpdateWorkersCompensation(w);
			
			Employee e = new Employee();
			e.Name = new Name("", "", "", "");
			e.PrintOnCheckAs = "";
			e.SSNo = "";
			e.Gender = 1;
			e.DateOfBirth = new DateTime(1982, 4, 20);
			e.Address = new Address("", "", "", "", "", "");
			e.State = 1;
			e.Phone = "";
			e.Cellular = "";
			e.AltPhone = "";
			e.Fax = "";
			e.Email = "";
			e.Pager = "";
			e.PinPager = "";
			e.AccountNo = "";
			e.BillingRateLevel = SessionFacade.ReadBillingRateLevelByPk(1);
			// TODO: Compensation
			e.HireDate = DateTime.Now;
			e.ReleaseDate = DateTime.Now.AddYears(20);
			e.Type = 1;
			e.WorkersCompensation = w;
			e.Inactive = false;
			SessionFacade.SaveOrUpdateEmployee(e);
		}
	}
}
